package com.yiren.dbaa.module.exam.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.dbaa.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by WANGRONGXIANG
 * on 2022/3/2
 * Describe 线路切换选择对话框
 */
public class LineChooseDialog extends Dialog {

    private Activity activity;
    private List<LineInfoEntity> lineInfos; //线路信息
    private int selectPosition = -1;   //选择的位置
    private int originPosition = -1;   //开始的位置

    private LineListAdapter lineListAdapter;  //线路列表适配器

    public LineChooseDialog(Activity activity, List<LineInfoEntity> lineInfos, LineInfoEntity lineInfo) {
        super(activity, R.style.Dialog);
        this.activity = activity;
        this.lineInfos = lineInfos;
        if (null != lineInfo) {
            for (int i = 0; i < lineInfos.size(); i++) {
                if (lineInfos.get(i).getId().equals(lineInfo.getId())) {
                    this.selectPosition = i;
                    this.originPosition = i;
                }
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置对话框使用的布局文件
        setContentView(R.layout.dialog_choose_line_or_student);
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        ListView lvContent = (ListView) findViewById(R.id.lv_content);
        Button btSure = (Button) findViewById(R.id.bt_sure);
        btSure.setText("确定");
        if (lineInfos.size() == 0) {
            tvTitle.setText("只有一条线路，请采集更多线路！");
        } else {
            tvTitle.setText("线路选择列表");
            lineListAdapter = new LineListAdapter();
            lvContent.setAdapter(lineListAdapter);
        }
        // item点击事件
        lvContent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectPosition = position;
                lineListAdapter.notifyDataSetChanged();
            }
        });
        //确定
        btSure.setOnClickListener(v -> {
            onLineSelectChangeListener.onLineSelectChange(selectPosition, selectPosition != originPosition);
            dismiss();
        });
    }

    /**
     * 线路列表适配器
     */
    class LineListAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            if (lineInfos != null && lineInfos.size() > 0) {
                return lineInfos.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return lineInfos.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LineInfoEntity lineInfo = lineInfos.get(position);
            ViewHolder holder;
            if (null == convertView) {
                convertView = LayoutInflater.from(activity).inflate(R.layout.item_line_choose, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tvLineName.setTextColor(activity.getResources().getColor(position == selectPosition ? R.color.dbaf_blue : R.color.dbaf_black));
            holder.tvLineName.setText(lineInfo.getName());
            return convertView;
        }

        class ViewHolder {
            @BindView(R.id.tv_line_name)
            TextView tvLineName;  //线路名称

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }

    private OnLineSelectChangeListener onLineSelectChangeListener;

    public void setOnLineSelectChangeListener(OnLineSelectChangeListener onLineSelectChangeListener) {
        this.onLineSelectChangeListener = onLineSelectChangeListener;
    }

    public interface OnLineSelectChangeListener {
        void onLineSelectChange(int position, boolean isChange);
    }

}
